package com.shxc.smarttrade.old.presenter;

import android.os.Handler;

import com.shxc.smarttrade.old.bean.AccountInfomation;
import com.shxc.smarttrade.old.bean.UserInfo;
import com.shxc.smarttrade.old.iview.IViewAccount;
import com.shxc.smarttrade.old.modal.AccountInfoModal;
import com.shxc.smarttrade.old.modal.IRequestServiceListener;

/**
 * Created by jcdev1 on 2017/6/8.
 */

public class AccountPresenter {
    private IViewAccount iViewAccount;
    private AccountInfoModal modal;
    Handler handler = new Handler();


    public AccountPresenter(IViewAccount iViewAccount) {
        this.iViewAccount = iViewAccount;
        modal = new AccountInfoModal();
    }

    /**
     * 获取账户详情
     *
     * @param userId
     */
    public void getAccountInfomation(String accessToken, int userId) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                iViewAccount.showDialog();
            }
        });
        modal.getAccountInfomation(accessToken, userId, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewAccount.setAccountInfomation((AccountInfomation.DataBean) result);
                        iViewAccount.dismissDialog();
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewAccount.showMyToast(msg);
                    }
                });
            }
        });
    }

    /**
     * 获取用户信息
     *
     * @param userId
     */
    public void getUserInfo(String accessToken, int userId, final int tag) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (tag == 0)
                    iViewAccount.showDialog();
            }
        });
        modal.getUserInfo(accessToken, userId, new IRequestServiceListener() {
            @Override
            public void success(final Object result) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewAccount.setUserInfo((UserInfo) result);
                        iViewAccount.dismissDialog();
                        iViewAccount.dismissrefresh();
                    }
                });
            }

            @Override
            public void faild(final String msg) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        iViewAccount.dismissDialog();
                        iViewAccount.showMyToast(msg);
                        iViewAccount.dismissrefresh();

                    }
                });
            }
        });
    }

}
